package com.hgyc.mom.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hgyc.mom.common.exception.BizException;
import com.hgyc.mom.tool.strategy.AutoCodeUtil;
import com.hgyc.mom.wms.entity.WmsPurchaseReceipt;
import com.hgyc.mom.wms.entity.WmsPurchaseReceiptDetail;
import com.hgyc.mom.wms.mapper.WmsPurchaseReceiptMapper;
import com.hgyc.mom.wms.service.WmsPurchaseReceiptDetailService;
import com.hgyc.mom.wms.service.WmsPurchaseReceiptService;
import com.hgyc.mom.wms.vo.WmsPurchaseReceiptVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 采购入库单主Service业务层实现
 *
 * @author fwj
 * @since 2025-07-08
 */
@Slf4j
@Service
public class WmsPurchaseReceiptServiceImpl extends ServiceImpl<WmsPurchaseReceiptMapper, WmsPurchaseReceipt> implements WmsPurchaseReceiptService {

    @Autowired
    private AutoCodeUtil autoCodeUtil;

    @Autowired
    private WmsPurchaseReceiptDetailService wmsPurchaseReceiptDetailService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveReceipt(WmsPurchaseReceiptVO wmsPurchaseReceiptVO) {
        wmsPurchaseReceiptVO.setReceiptCode(autoCodeUtil.genSerialCode("purchase_receipt_code", null));
        save(wmsPurchaseReceiptVO);

        //新增物料入库单行
        List<WmsPurchaseReceiptDetail> details = wmsPurchaseReceiptVO.getAddDetails();
        if (!CollectionUtils.isEmpty(details)) {
            details.forEach(item -> {
                item.setId(null);
                item.setReceiptId(wmsPurchaseReceiptVO.getId());
            });
            wmsPurchaseReceiptDetailService.saveBatch(details);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateReceipt(WmsPurchaseReceiptVO wmsPurchaseReceiptVO) {
        // TODO 其他逻辑
        updateById(wmsPurchaseReceiptVO);

        //删除物料入库单行
        if (!CollectionUtils.isEmpty(wmsPurchaseReceiptVO.getDeleteDetailIds())) {
            wmsPurchaseReceiptDetailService.removeBatchByIds(wmsPurchaseReceiptVO.getDeleteDetailIds());
        }

        //新增物料入库单行
        List<WmsPurchaseReceiptDetail> details = wmsPurchaseReceiptVO.getAddDetails();
        if (!CollectionUtils.isEmpty(details)) {
            details.forEach(item -> {
                item.setId(null);
                item.setReceiptId(wmsPurchaseReceiptVO.getId());
            });
            wmsPurchaseReceiptDetailService.saveBatch(details);
        }

        //更新物料入库单行
        if (!CollectionUtils.isEmpty(wmsPurchaseReceiptVO.getUpdateDetails())) {
            List<WmsPurchaseReceiptDetail> updateDetails = wmsPurchaseReceiptVO.getUpdateDetails();
            wmsPurchaseReceiptDetailService.updateBatchById(updateDetails);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeReceipt(List<Long> ids) {
        // TODO 其他逻辑
        removeByIds(ids);
        //删除物料入库单行
        wmsPurchaseReceiptDetailService.deleteBatchByParentIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<WmsPurchaseReceipt> wmsPurchaseReceiptList, boolean updateSupport) {
        if (wmsPurchaseReceiptList == null || wmsPurchaseReceiptList.isEmpty()) {
            throw new IllegalArgumentException("导入数据不能为空");
        }

        try {
            if (updateSupport) {
                // TODO 实现更新已有数据的导入逻辑
                return "更新导入成功，共导入 " + wmsPurchaseReceiptList.size() + " 条数据";
            } else {
                // TODO 实现不更新已有数据的导入逻辑
                return "新增导入成功，共导入 " + wmsPurchaseReceiptList.size() + " 条数据";
            }
        } catch (Exception e) {
            log.error("采购入库单主导入失败", e);
            throw new BizException("采购入库单主导入失败：" + e.getMessage());
        }
    }
}
